frappe.pages['gantt_demo'].on_page_load = function(wrapper) {
	var page = frappe.ui.make_app_page({
		parent: wrapper,
		title: '甘特图演示',
		single_column: true
	});
	page.add_inner_button('刷新', () => load_gantt_data());		
	console.log(wrapper.id);
	$("head").append("<link rel='stylesheet' href='/assets/frappe_study/lib/dhtmlxgantt.css'>");
	//$("<div id='gantt_here' style='height: 500px;'></div>").appendTo('.layout-main-section');
	$("<div id='gantt_here' style='height: 500px;'></div>").appendTo(page.main);
	//中文语言
	gantt.i18n.setLocale("cn");	
	//刻度
	gantt.config.scales = [
		{unit: "month", step: 1, format: "%F, %Y"},		
		{unit: "day", step: 1, date: "%j,周%D"}
	];	
	gantt.config.grid_width = 400; // 设置左侧网格的宽度
	//定义列
	gantt.config.columns =  [
		{name:"text",       label:"人员/项目", align:"left",  tree:true ,width:150},		
		{name:"start_date", label:"开始日期", align:"center" ,width:100},
		{name:"end_date", label:"结束日期", align:"center",width:100},
		//{name:"duration",   label:"持续天数", align:"center" },	
		{name: "buttons",label: "详情", align:"center",width: 50,template: function (task) {
			//console.log(task);
			if (task.$level == 0)
			{
				return ('<i class="fa fa-user" data-action="more"></i>');
			}
			else
			{
				return ''
			}			
		}}
	];
	//关联事件
	gantt.attachEvent("onTaskClick", function(id, e){
		var button = e.target.closest("[data-action]")
		if(button){
			var action = button.getAttribute("data-action");
			switch (action) {
				case "more":
					console.log(id);
					window.open("/app/work_person/"+id, "_blank");					
					break;				
			}
			return false;
		}
		return true;
	});

	
	// 设置日期格式为年-月-日
	gantt.config.date_format = "%Y-%m-%d";
	
	gantt.plugins({
		marker: true
	});
	var dateToStr = gantt.date.date_to_str(gantt.config.task_date);
	var today = new Date();
	gantt.addMarker({
		start_date: today,
		css: "today",
		text: "今天",
		title: "今天: " + dateToStr(today)
	});


	gantt.config.start_date = new Date();
	// 获取当前日期
	var currentDate = new Date();
	// 将当前日期向后推迟两个月
	currentDate.setMonth(currentDate.getMonth() + 2); 
	gantt.config.end_date = currentDate;
	gantt.config.show_tasks_outside_timescale = true

	gantt.init("gantt_here");	
	var tasks = {
        data: [
            {
                id: 1, text: "倒霉熊", start_date: "2024-04-01", duration: 30, order: 10,
                progress: 0.4, open: true,render:"split"
            },
            {
                id: 2, text: "项目1", start_date: "2024-04-01", duration: 8, order: 10,
                progress: 0.6, parent: 1
            },
            {
                id: 3, text: "项目2", start_date: "2024-04-15", duration: 15, order: 20,
                progress: 0.6, parent: 1
            }
        ],
        links: [
            {id: 1, source: 1, target: 2, type: "1"},
            {id: 2, source: 2, target: 3, type: "0"}
        ]
    };
	//gantt.parse(tasks);	
	
	// frappe.db.get_list('work_plan', {
	// 	fields: ['project', 'work_person','start_dt', 'end_dt'],
	// 	limit_start:0,
	// 	limit_page_length:1000
	// }).then(records => {
	// 	console.log(records);
	// })
	load_gantt_data();
}

function load_gantt_data()
{
	frappe.call('frappe_study.frappestudy.work_plan_service.get_work_plan_report')
    .then(r => {
        console.log(r);
		r.data.forEach(function(obj) {
			obj.text = obj.project; 
			obj.start_date = obj.start_dt;
			obj.end_date = obj.end_dt;
			obj.id = obj.name;
		  });
		console.log(r);
		gantt.parse(r);
        // {message: "pong"}
    })
}